package com.hua.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSON;
import com.hua.utils.CityDatasource;

public class CountryServlet extends HttpServlet {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	/**
	 * 
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

		doPost(request, response);
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setContentType("text/html;charset=utf-8");
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");// 设置编码
		PrintWriter write = response.getWriter();
		String type = request.getParameter("type");
		if ("json".equals(type)) {
			List<String> list = CityDatasource.getCityList();// 获得数据集合
			Map<String, List<String>> map = new HashMap<String, List<String>>();
			map.put("citys", list);
			String jsonString = null;
			jsonString = JSON.toJSONString(map).toString();
			System.out.println(jsonString);
			write.println(jsonString);
		} else if ("xml".equals(type)) {
			String jsonString = CityDatasource.getCityListForXml();
			System.out.println(jsonString);
			write.println(jsonString);
		}
		write.flush();
		write.close();
	}

}
